﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DataClassLayer
{
    public class StadiumCollection
    {
        public StadiumCollection()
        {
            
        }
        public bool Add(Stadium stadium)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Insert into SOCCER_STADIUMS (Name,DateOfFoundation,Capacity,GeneralInformation,Image) Values
                   ('" + stadium.Name + "',Convert(datetime,'"
                   + stadium.DateOfFoundation + "',103),"
                   + stadium.Capacity + ",'"
                    + stadium.GeneralInformation + "','"
                   + stadium.Image + "')";
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Edit(Stadium stadium)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Update SOCCER_STADIUMS Set 
                     Name='" + stadium.Name +  
                     "',DateOfFoundation=Convert(datetime,'" + stadium.DateOfFoundation + 
                     "',103),Capacity=" + stadium.Capacity + 
                     ",GeneralInformation='" + stadium.GeneralInformation +
                     "',Image='" + stadium.Image +
                     "' Where ID=" + stadium.ID;
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool Delete(Stadium stadium)
        {
            try
            {
                List<string> query = new List<string>();
                string command = @"Delete From SOCCER_STADIUMS Where ID= " + stadium.ID; 
                query.Add(command);
                DataAccessObject.DataBase.CreateUpdateDelete(query);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public DataTable FillList(Stadium stadium)
        {
            try
            {
                string wherestatement = WhereStatement(stadium);
                string command = @"SELECT        ID, Name, Capacity, DateOFFoundation, GeneralInformation,Image
                                   FROM            SOCCER_STADIUMS";
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    command += " Where " + wherestatement;
                }
                DataTable table = DataAccessObject.DataBase.ExcuteQuery(command);
                return table;
            }
            catch
            {
                return null;
            }
        }
        public int MaxID()
        {
            try
            {
                int Maxid = 1;
                string command = @"SELECT    Max(ID) AS MaxID FROM   SOCCER_STADIUMS";
                DataTable table = DataAccessObject.DataBase.ExcuteQuery(command);
                if (table.Rows.Count == 1)
                {
                    Maxid = int.Parse(table.Rows[0]["MaxID"].ToString());
                }
                return Maxid;
            }
            catch
            {
                return 1;
            }
        }
        public string WhereStatement(Stadium stadium)
        {
            string wherestatement = "";
            if (stadium.ID > 0)
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And ID= " + stadium.ID;
                }
                else
                {
                    wherestatement += "ID= " + stadium.ID;
                }
            }
            if (stadium.Name != null &&
                stadium.Name != "")
            {
                if (wherestatement != null &&
                    wherestatement != "")
                {
                    wherestatement += "And Name Like '%" + stadium.Name + "%' ";
                }
                else
                {
                    wherestatement += " Name Like '%" + stadium.Name + "%' ";
                }
            }
            return wherestatement;
        }
    }
}
